#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());
        vector<vector<int>> vv1;

        if (nums.size() < 2)
            return {};
        int a = 0;
        int b = 1;
        int flag = 1;
        // if(target==294967296)
        //     return {};
        while (a < nums.size() && nums[a] + nums[b] <= target / 2)
        {
            int b = a + 1;
            while (b < nums.size())
            {
                int left = b + 1;
                int right = nums.size() - 1;
                while (left < right)
                {
                    long long sum = (long long)target - (nums[a] + nums[b]);
                    while (left < right && nums[left] + nums[right] < sum)
                        left++;
                    while (left<right && nums[left] + nums[right]>sum)
                        right--;
                    if (left < right && nums[left] + nums[right] == sum)
                    {
                        vv1.push_back({ nums[a],nums[b],nums[left],nums[right] });
                        left++;
                        right--;
                    }
                    while (left < right && nums[left] == nums[left - 1])
                        left++;
                    while (left < right && nums[right] == nums[right + 1])
                        right--;
                }
                b++;
                while (b < nums.size() && nums[b] == nums[b - 1])
                    b++;
            }
            a++;
            while (a < nums.size() && nums[a] == nums[a - 1])
                a++;
        }
        return vv1;
    }
};